package com.xw.mailservice.listener;

import com.xw.mailservice.task.GetMailTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import java.security.Security;

/**
 * Spring启动完成监听器
 * @author 杨建权
 * @create 2017-12-11 9:40
 **/
public class SpringStartedEventListener implements ApplicationListener<ApplicationReadyEvent>{

    static final Logger logger = LoggerFactory.getLogger(SpringStartedEventListener.class);

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        logger.info("SpringBoot启动完成！！！");
        init( event.getApplicationContext());
    }

    /**
     * 读取配置文件获取邮箱相关的配置信息
     * @param applicationContext
     */
    private void init(ApplicationContext applicationContext) {
        GetMailTask gmt = applicationContext.getBean("getMailTask", GetMailTask.class);
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        //判断是否需要开启邮件收取任务
        if (gmt.checkIsNeedToFatchMailWhenStart()) {
            //启动邮件获取任务
            gmt.start();
        }
    }
}
